package com.hutao.manage.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateHelper {
	private static final SimpleDateFormat yyyymmdd = new SimpleDateFormat(
			"yyyy-MM-dd");
	private static final SimpleDateFormat yyyymmddhhmmss = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	private static final SimpleDateFormat yyyymmddhhmmsslian = new SimpleDateFormat(
			"yyyyMMdd");
	private static final SimpleDateFormat yyyymmddhhmmss00 = new SimpleDateFormat(
			"yyyy-MM-dd 00:00:00");
	private static final SimpleDateFormat yyyymmddhhmmss59 = new SimpleDateFormat(
			"yyyy-MM-dd 23:59:59");

	public static Date getStartDate(Date start) {
		String dateStr = yyyymmddhhmmss00.format(start);
		try {
			return yyyymmddhhmmss.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static Date getEndDate(Date end) {
		String dateStr = yyyymmddhhmmss59.format(end);
		try {
			return yyyymmddhhmmss.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取当前日期增加几天的日期
	 * 
	 * @param start
	 * @param end
	 * @param chooseDay
	 * @return
	 */
	public static Date getDateByChooseDaynum(Date start, int chooseDay) {
		Calendar now = Calendar.getInstance();
		now.setTime(start);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + chooseDay);
		return now.getTime();

	}

	/**
	 * 获取间隔天数
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public static int getBetweenDateNum(Date start, Date end) {
		long time = end.getTime() - start.getTime();
		return (int) (time / 1000 / 60 / 60 / 24);
	}

	public static Timestamp getTimeStampByString(String timeStampStr) {
		if (null == timeStampStr) {
			return null;
		}
		Timestamp rtTimeStamp = null;
		try {
			Date date = yyyymmddhhmmss.parse(timeStampStr);
			rtTimeStamp = new Timestamp(date.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return rtTimeStamp;
	}

	public static Calendar setYearTou(Calendar calendar) {
		calendar.set(Calendar.MONTH, 0);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);// 秒
		calendar.set(Calendar.MILLISECOND, 0);// 毫秒
		return calendar;
	}



	/**
	 * 据今年获取相差多少年
	 * 
	 */
	public static int chaNian(Date birthDay) {
		int i = 0;
		if (null == birthDay) {
			return i;
		}
		Calendar nowCalendar = Calendar.getInstance();
		Calendar birthdayCalendar = Calendar.getInstance();
		birthdayCalendar.setTime(birthDay);
		i = nowCalendar.get(Calendar.YEAR)
				- birthdayCalendar.get(Calendar.YEAR);
		if (i < 0) {
			return 0;
		}
		return i;
	}

	/**
	 * 入院日期减出生得到相差多少年
	 * 
	 */
	public static int chaNianry(Date birthDay, Date ryrq) {
		int i = 1;
		if (null == birthDay) {
			return i;
		}
		if (null == ryrq) {
			return i;
		}
		// Calendar nowCalendar = Calendar.getInstance();
		Calendar birthdayCalendar = Calendar.getInstance();
		Calendar birthdayCalendarry = Calendar.getInstance();
		birthdayCalendar.setTime(birthDay);
		birthdayCalendarry.setTime(ryrq);
		i = birthdayCalendarry.get(Calendar.YEAR)
				- birthdayCalendar.get(Calendar.YEAR);
		if (i < 0) {
			return 1;
		}
		return i;
	}

	/**
	 * 不足一年按月来算
	 * 
	 */
	public static Integer chaBuZuNian(Date birthDay) {
		Integer i = null;
		if (null == birthDay) {
			return i;
		}

		Calendar nowCalendar = Calendar.getInstance();
		Calendar birthdayCalendar = Calendar.getInstance();
		birthdayCalendar.setTime(birthDay);
		int nowyear = nowCalendar.get(Calendar.YEAR);
		int birthYear = birthdayCalendar.get(Calendar.YEAR);
		if (nowyear != birthYear) {
			int niancha = nowyear - birthYear;
			if (niancha > 0) {
				niancha *= 12;
				i = nowCalendar.get(Calendar.MONTH)
						- birthdayCalendar.get(Calendar.MONTH);
				i += niancha;
				if (i >= 12) {
					i = null;
				}
			}
		} else {
			i = nowCalendar.get(Calendar.MONTH)
					- birthdayCalendar.get(Calendar.MONTH);
		}
		return i;
	}
	
	/**
	 * 不足一年按月来算
	 * 
	 */
	public static Integer chaBuZuNian_20170426(Date birthDay, Timestamp rytime) {
		Integer i = null;
		if (null == birthDay) {
			return i;
		}
		//不足一月的年龄为0
		Double month_cha=chaBuZuNian(birthDay, rytime);
		if(month_cha!=null&&month_cha<1){
			return 0;
		}
				
		Calendar nowCalendar = Calendar.getInstance();
		nowCalendar.setTime(rytime);
		Calendar birthdayCalendar = Calendar.getInstance();
		birthdayCalendar.setTime(birthDay);
		int nowyear = nowCalendar.get(Calendar.YEAR);
		int birthYear = birthdayCalendar.get(Calendar.YEAR);
		if (nowyear != birthYear) {
			int niancha = nowyear - birthYear;
			if (niancha > 0) {
				niancha *= 12;
				i = nowCalendar.get(Calendar.MONTH)
						- birthdayCalendar.get(Calendar.MONTH);
				i += niancha;
				if (i >= 12) {
					i = null;
				}
			}
		} else {
			i = nowCalendar.get(Calendar.MONTH)
					- birthdayCalendar.get(Calendar.MONTH);
		}
		return i;
	}

	/**
	 * 不足一年按月来算
	 * 
	 */
	public static Double chaBuZuNian(Date birthDay, Date zysj) {
		Double i = null;
		if (null == birthDay) {
			return i;
		}
		if (null == zysj) {
			return i;
		}

		long zysjLong = zysj.getTime();
		long berLong = birthDay.getTime();
		Long cha = zysjLong - berLong;

		i = (double) ((cha / 1000 / 60 / 60 / 24) / 30.0);
		if (i > 12) {
			i = null;
		}

		return i;
	}

	/**
	 * 住院天数
	 * 
	 */
	public static Long getZYTS(Date cysj, Date rysj) {
		Long i = null;
		if (null == cysj || null == rysj) {
			return i;
		}
		long cysjMil = cysj.getTime();
		long rysjMil = rysj.getTime();
		Calendar rysjCal = Calendar.getInstance();
		rysjCal.setTimeInMillis(rysjMil);
		rysjCal.set(Calendar.HOUR_OF_DAY, 0);
		rysjCal.set(Calendar.MINUTE, 0);
		rysjCal.set(Calendar.SECOND, 0);
		Calendar cysjCal = Calendar.getInstance();
		cysjCal.setTimeInMillis(cysjMil);
		cysjCal.set(Calendar.HOUR_OF_DAY, 0);
		cysjCal.set(Calendar.MINUTE, 0);
		cysjCal.set(Calendar.SECOND, 0);
		i = (cysjCal.getTimeInMillis() - rysjCal.getTimeInMillis()) / 1000 / 60
				/ 60 / 24;
		if (i == 0) {
			return 1L;
		}
		return i;
	}
	
	public static String getTS(Date birthDay, Timestamp rytime) {
		Long i = null;
		if (null == birthDay || null ==rytime ) {
			return null;
		}
		long cysjMil = rytime.getTime();
		long rysjMil = birthDay.getTime();
		Calendar rysjCal = Calendar.getInstance();
		rysjCal.setTimeInMillis(rysjMil);
		rysjCal.set(Calendar.HOUR_OF_DAY, 0);
		rysjCal.set(Calendar.MINUTE, 0);
		rysjCal.set(Calendar.SECOND, 0);
		Calendar cysjCal = Calendar.getInstance();
		cysjCal.setTimeInMillis(cysjMil);
		cysjCal.set(Calendar.HOUR_OF_DAY, 1);
		cysjCal.set(Calendar.MINUTE, 0);
		cysjCal.set(Calendar.SECOND, 0);
//		System.out.print(cysjCal.getTimeInMillis()-rysjCal.getTimeInMillis());
		i = (cysjCal.getTimeInMillis() - rysjCal.getTimeInMillis()) / 1000 / 60
				/ 60 / 24;
		if (i <30) {
			return i+"/30";
		}
		return i/30+" "+i%30+"/30";
	}

	public static void main(String[] args) throws ParseException {
/*		 System.out.println(yyyymmddhhmmss.format(getThisDayStart()));
		 String tsStr = "2018-09-30 16:49:45";
		 Timestamp ts = null;
		 try {
		  ts = Timestamp.valueOf(tsStr);
		 System.out.println(ts);
		 } catch (Exception e) {
		 e.printStackTrace();
		 }
		 System.out.print(getTS(getThisDayStart(),ts));
		 */
		String str="370123";
		System.out.print(str.substring(0,4));
		System.out.println(getZYTSForInt(new Date(),new Date()));
	}

	public static Integer getZYTSForInt(Date cysj, Date rysj) {
		Integer i = null;
		if (null == cysj || null == rysj) {
			return i;
		}
		long cysjMil = cysj.getTime();
		long rysjMil = rysj.getTime();
		Calendar rysjCal = Calendar.getInstance();
		rysjCal.setTimeInMillis(rysjMil);
		rysjCal.set(Calendar.HOUR_OF_DAY, 0);
		rysjCal.set(Calendar.MINUTE, 0);
		rysjCal.set(Calendar.SECOND, 0);
		Calendar cysjCal = Calendar.getInstance();
		cysjCal.setTimeInMillis(cysjMil);
		cysjCal.set(Calendar.HOUR_OF_DAY, 0);
		cysjCal.set(Calendar.MINUTE, 0);
		cysjCal.set(Calendar.SECOND, 0);
		i = (int) ((cysjCal.getTimeInMillis() - rysjCal.getTimeInMillis()) / 1000 / 60 / 60 / 24);
		if (i == 0) {
			return 1;
		}
		return i;
	}
	
	public static Integer getZYTSForCha(Date cysj, Date rysj) {
		Integer i = null;
		if (null == cysj || null == rysj) {
			return i;
		}
		long cysjMil = cysj.getTime();
		long rysjMil = rysj.getTime();
		Calendar rysjCal = Calendar.getInstance();
		rysjCal.setTimeInMillis(rysjMil);
		rysjCal.set(Calendar.HOUR_OF_DAY, 0);
		rysjCal.set(Calendar.MINUTE, 0);
		rysjCal.set(Calendar.SECOND, 0);
		Calendar cysjCal = Calendar.getInstance();
		cysjCal.setTimeInMillis(cysjMil);
		cysjCal.set(Calendar.HOUR_OF_DAY, 0);
		cysjCal.set(Calendar.MINUTE, 0);
		cysjCal.set(Calendar.SECOND, 0);
		i = (int) ((cysjCal.getTimeInMillis() - rysjCal.getTimeInMillis()) / 1000 / 60 / 60 / 24);
		return i;
	}

	public static Integer getZYTSForIntHours(Date cysj, Date rysj) {
		Integer i = null;
		if (null == cysj || null == rysj) {
			return i;
		}
		long cysjMil = cysj.getTime();
		long rysjMil = rysj.getTime();
		Calendar rysjCal = Calendar.getInstance();
		rysjCal.setTimeInMillis(rysjMil);

		Calendar cysjCal = Calendar.getInstance();
		cysjCal.setTimeInMillis(cysjMil);
		i = (int) ((cysjCal.getTimeInMillis() - rysjCal.getTimeInMillis()) / 1000 / 60 / 60);
		return i;
	}

	/**
	 * 返回增加一天的字符串
	 * 
	 * @param oldDay
	 * @return
	 */
	public static String addOneDay(String oldDay) {
		String newDay = null;
		try {
			Date oldDate = getyyyyMMdd().parse(oldDay);
			Calendar nowCalendar = Calendar.getInstance();
			nowCalendar.setTime(oldDate);
			nowCalendar.add(Calendar.DAY_OF_MONTH, 1);
			newDay = getyyyyMMdd().format(nowCalendar.getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return newDay;
	}

	/**
	 * 返回增加一天的字符串
	 * 
	 * @param oldDay
	 * @return
	 */
	public static String addOneDay(Date oldDay) {
		String newDay = null;
		Calendar nowCalendar = Calendar.getInstance();
		nowCalendar.setTime(oldDay);
		nowCalendar.add(Calendar.DAY_OF_MONTH, 1);
		newDay = getyyyyMMdd().format(nowCalendar.getTime());
		return newDay;
	}

	public static String getStrDateyyyy_MM_dd(Timestamp tim) {
		if (null != tim) {
			Date date = new Date(tim.getTime());
			return getyyyyMMdd().format(date);
		}
		return null;
	}

	public static long handlerDate_chaNotTime(Timestamp bigTmp,
			Timestamp smallTmp) {
		if (bigTmp == null || smallTmp == null) {
			return -1;
		}
		long between_days = (bigTmp.getTime() - smallTmp.getTime())
				/ (1000 * 3600 * 24);
		return between_days;
	}

	public static Date getThisDayStart() {
		Calendar cysjCal = Calendar.getInstance();
		cysjCal.set(Calendar.HOUR_OF_DAY, 0);
		cysjCal.set(Calendar.MINUTE, 0);
		cysjCal.set(Calendar.SECOND, 0);
		return new Date(cysjCal.getTimeInMillis());
	}

	public static Date getThisDayEnd() {
		Calendar cysjCal = Calendar.getInstance();
		cysjCal.set(Calendar.HOUR_OF_DAY, 23);
		cysjCal.set(Calendar.MINUTE, 59);
		cysjCal.set(Calendar.SECOND, 59);
		return new Date(cysjCal.getTimeInMillis());
	}

	public static SimpleDateFormat getyyyyMMdd() {
		return yyyymmdd;
	}

	public static SimpleDateFormat getyyyyMMddhhssmm() {
		return yyyymmddhhmmss;
	}

	public static SimpleDateFormat getYyyymmddhhmmss() {
		return yyyymmddhhmmss;
	}

	public static SimpleDateFormat getYyyymmddhhmmsslian() {
		return yyyymmddhhmmsslian;
	}

	public static SimpleDateFormat getYyyymmddhhmmss00() {
		return yyyymmddhhmmss00;
	}

	public static SimpleDateFormat getYyyymmddhhmmss59() {
		return yyyymmddhhmmss59;
	}

}
